<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->

      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="500px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="医生名字" prop="doctorName">
            <el-input v-model="form.doctorName" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="所属科室" prop="departmentId">

            <el-select v-model="form.departmentId" filterable placeholder="请选择">
              <el-option
                v-for="item in departments"
                :key="item.departmentId"
                :label="item.department"
                :value="item.departmentId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="挂号类型" prop="registeredId">
            <el-select v-model="form.registeredId" filterable placeholder="请选择">
              <el-option
                v-for="item in registeredType"
                :key="item.registeredTypeId"
                :label="item.type"
                :value="item.registeredTypeId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="dState">
            <el-input v-model="form.dState" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="上午值班开始时间">
            <el-input v-model="form.amStartTime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="上午值班结束时间">
            <el-input v-model="form.amEndTime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="下午值班开始时间">
            <el-input v-model="form.pmStartTime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="下午值班结束时间">
            <el-input v-model="form.pmEndTime" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->

      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="doctorId" label="医生id" />
        <el-table-column prop="doctorName" label="医生名字" />
        <el-table-column prop="departmentId" label="所属科室">
          <template slot-scope="scope">
            {{ a(scope.row.departmentId) }}
          </template>
        </el-table-column>
        <el-table-column prop="registeredId" label="挂号类型">
          <template slot-scope="scope">
            {{ b(scope.row.registeredId) }}
          </template>
        </el-table-column>
        <el-table-column prop="dState" label="dState" />
        <el-table-column prop="amStartTime" label="上午值班开始时间" />
        <el-table-column prop="amEndTime" label="上午值班结束时间" />
        <el-table-column prop="pmStartTime" label="下午值班开始时间" />
        <el-table-column prop="pmEndTime" label="下午值班结束时间" />
        <el-table-column v-if="checkPer(['admin', 'doctor:edit', 'doctor:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation :data="scope.row" :permission="permission" />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudDoctor from '@/api/his/doctor'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'


import { initData } from '@/api/data'

const defaultForm = { doctorId: null, doctorName: null, departmentId: null, registeredId: null, dState: null, amStartTime: null, amEndTime: null, pmStartTime: null, pmEndTime: null, createBy: null, updateBy: null, createTime: null, updateTime: null }
export default {
  name: 'Doctor',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: 'his:医生表', url: 'api/doctor', idField: 'doctorId', sort: 'doctorId,desc', crudMethod: { ...crudDoctor } })
  },
  data() {
    return {

      departments: [],
      registeredType: [],
      permission: {
        add: ['admin', 'doctor:add'],
        edit: ['admin', 'doctor:edit'],
        del: ['admin', 'doctor:del']
      },
      rules: {
        doctorName: [
          { required: true, message: '医生名字不能为空', trigger: 'blur' }
        ],
        departmentId: [
          { required: true, message: '所属科室不能为空', trigger: 'blur' }
        ],
        registeredId: [
          { required: true, message: '挂号类型不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.queryDepartments()
    this.queryRegisteredType()
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },


    // 初始化加载数据
    queryDepartments() {
      initData('/api/departments', 'departmentId').then(res => {
        this.departments = res.content
      }).catch(error => {

      })
    },
    queryRegisteredType() {
      initData('/api/registeredType', 'registeredTypeId').then(res => {
        this.registeredType = res.content
      }).catch(error => {

      })
    },
    // 转化属性
    a(data) {
      for (const item of this.departments) {
        if (data == item.departmentId) {
          return item.department
        }
      }
    },
    // 转化属性
    b(data) {
      for (const item of this.registeredType) {
        if (data == item.registeredTypeId) {
          return item.type
        }
      }
    }

  }
}
</script>

<style scoped>

</style>
